<html>
<head>
<script>
var currStyle;
if (!localStorage.mode) {
localStorage.mode = "dark";
localStorage.version = 2;
chrome.tabs.create({url:"options.html"});
}

currStyle = localStorage.mode;

function onRequest(request, sender, sendResponse) {
   //if (request == "initialize") {
      chrome.pageAction.show(sender.tab.id);
      
      sendResponse({mode: currStyle});
   //}
}
         
function changeStyle(){
   var newStyle;
   if (currStyle == "light")
      newStyle = "dark";
   else
      newStyle = "light";
   
   chrome.tabs.executeScript(null, {code:"switchStyle('"+newStyle+"')"});   
   currStyle = newStyle;
   localStorage.mode = newStyle;
   
   _gaq.push(['_setCustomVar', 1, 'ColorScheme', currStyle, 3]);
   _gaq.push(['_trackPageview']);
   _gaq.push(['_trackEvent', 'ColorScheme', currStyle]);
}

chrome.pageAction.onClicked.addListener(changeStyle);
chrome.extension.onRequest.addListener(onRequest);


     
</script>
</head>
<body>
   <script>
     var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-16933741-1']);
     _gaq.push(['_setCustomVar', 1, 'ColorScheme', currStyle, 3]);
     _gaq.push(['_trackPageview']);
     _gaq.push(['_trackEvent', 'ReaderPage', 'loaded']);

     (function() {
       var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
       ga.src = 'https://ssl.google-analytics.com/ga.js';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
     })();
   </script>
</body>
</html>